package com.sloan.community.controller;

import com.sloan.community.common.Login;
import com.sloan.community.common.command.CommentCommand;
import com.sloan.community.common.res.Result;
import com.sloan.community.common.vo.CommentVO;
import com.sloan.community.common.vo.DelCommentVO;
import com.sloan.community.po.User;
import com.sloan.community.service.CommentService;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;

@Slf4j
@Controller
@RequestMapping("comment")
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class CommentController {

    private final CommentService commentService;

    @ResponseBody
    @GetMapping("list/{articleId}")
    public Mono<Result<List<CommentVO>>> articleCommentList(@PathVariable("articleId") Long articleId, User user) {
        Flux<CommentVO> commentFlux = commentService.articleCommentList(articleId, user);
        return commentFlux.collectList().map(Result::success);
    }

    @Login(required = true)
    @ResponseBody
    @PostMapping("{articleId}")
    public Mono<Result<CommentVO>> add(@PathVariable("articleId") @Valid @Min(value = 1, message = "文章不能为空")
                                       @NotNull(message = "文章不能为空") Long articleId, User user,
                                       @RequestBody @Valid CommentCommand commentCommand) {
        Mono<CommentVO> addCommentMono = commentService.add(articleId, user, commentCommand);
        return addCommentMono.map(Result::success);
    }

    @Login
    @ResponseBody
    @PutMapping("like-count/{commentId}")
    public Mono<Result<Long>> likeCount(@PathVariable("commentId") @Valid @Min(value = 1, message = "评论ID不能为空")
                                        @NotNull(message = "评论ID不能为空") Long commentId, User user) {
        Mono<Long> addCommentMono = commentService.likeCount(commentId, user);
        return addCommentMono.map(Result::success);
    }

    @ResponseBody
    @GetMapping("comment-chat/{commentId}")
    public Mono<Result<List<CommentVO>>> commentChat(@PathVariable("commentId") @Valid @Min(value = 1, message = "评论ID不能为空")
                                                     @NotNull(message = "评论ID不能为空") Long commentId, User user) {
        Flux<CommentVO> addCommentMono = commentService.commentChat(commentId, user);
        return addCommentMono.collectList().map(Result::success);
    }

    @Login
    @ResponseBody
    @DeleteMapping("{commentId}")
    public Mono<Result<DelCommentVO>> delete(@PathVariable("commentId")
                                     @Valid @Min(value = 1, message = "评论ID不能为空")
                                     @NotNull(message = "评论ID不能为空") Long commentId,User user) {
        return commentService.delete(commentId,user)
                .map(Result::success);
    }
}
